using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Speed;

namespace SpeedSearch.FullText.Parsing
{

    public static class TesseractExe
    {

        public static void GetText(string fileName, string outPut)
        {
            string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tessocr-portable");
            string exeName = "tesseract.exe";

            // um pequeno ajuste, por o tesseract adiona uma extensão .txt, mesmo que já seja .txt
            string outPut2 = Path.Combine(Path.GetDirectoryName(outPut), Path.GetFileNameWithoutExtension(outPut));
            string args = string.Format("\"{0}\" \"{1}\" -l por", fileName, outPut2);

            try
            {
                ProcessStartInfo pi = new ProcessStartInfo(exeName, args);
                pi.CreateNoWindow = true;
                pi.WorkingDirectory = dir;
                pi.UseShellExecute = true;
                pi.WindowStyle = ProcessWindowStyle.Hidden;
                //pi.Domain;
                //pi.UserName;
                //pi.Password;
                pi.ErrorDialog = false;

                string dirOut = Path.GetDirectoryName(outPut2);
                if (!Directory.Exists(dirOut))
                    Directory.CreateDirectory(dirOut);

                if (File.Exists(outPut))
                    File.Delete(outPut);
                Process.Start(pi).WaitForExit();
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Indexação: {0}", outPut);
            }
        }

    }

}
